package com.ruoyi.web.controller.api;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.shop.domain.*;
import com.ruoyi.shop.service.*;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.api.vo.CategoryTreeVo;
import com.ruoyi.web.controller.api.vo.CategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api("B端相关接口")
@RestController
@RequestMapping("/base")
public class BaseShopController extends BaseController {


    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IOrdersService ordersService;
    @Autowired
    private IOrdersGoodsService ordersGoodsService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IUserGoodsSpecialService userGoodsSpecialService;
    @Autowired
    private ISysUserService userService;


    @ApiOperation("获取商品分类树形结构")
    @GetMapping("/getCategoryTree")
    public R<List<CategoryTreeVo>> getCategoryTree() {
        List<CategoryTreeVo> categoryTreeVoList = new ArrayList<>();

        List<Category> list =  categoryService.selectCategoryList(new Category());
        list.forEach(item->{
            CategoryTreeVo categoryTreeVo = new CategoryTreeVo();
            Category category = new Category();
            category.setParentId(item.getId());
            List<TreeBaseVo> childrenList = categoryService.selectTreeBaseList(category);
            categoryTreeVo.setId(item.getId());
            categoryTreeVo.setLabel(item.getClassName());
            categoryTreeVo.setValue(item.getId().toString());
            categoryTreeVo.setImages(item.getImages());
            categoryTreeVo.setChildren(childrenList);
            categoryTreeVoList.add(categoryTreeVo);
        });
        return R.ok(categoryTreeVoList);
    }

    @ApiOperation("根据用户ID 查询用户特价商品信息")
    @GetMapping("/getUserSpecialGoods")
    public R<List<Goods>> getUserSpecialGoods(@RequestParam Long userId) {
        UserGoodsSpecial userGoodsSpecial = new UserGoodsSpecial();
        userGoodsSpecial.setUserId(userId);
        List<Goods> list = goodsService.selectUserSpecialGoods(userGoodsSpecial);
        return R.ok(list);
    }


    @ApiOperation("保存用户特价信息")
    @PostMapping("/saveUserSpecialGoods")
    public R<Integer> saveUserSpecialGoods(@RequestBody List<UserGoodsSpecial> userGoodsSpecials) {
        for (UserGoodsSpecial userGoodsSpecial : userGoodsSpecials) {
            if(userGoodsSpecial.getSpecialPrice()!=null && userGoodsSpecial.getSpecialPrice().doubleValue()>0){
                //判断是否存在该条记录
                List<UserGoodsSpecial> list =userGoodsSpecialService.selectUserGoodsSpecialList(userGoodsSpecial);
                if(list!=null && list.size()>0){
                    //更新
                    userGoodsSpecial.setId(list.get(0).getId());
                    userGoodsSpecial.setGoodsName(goodsService.selectGoodsById(userGoodsSpecial.getGoodsId()).getGoodName());
                    userGoodsSpecialService.updateUserGoodsSpecial(userGoodsSpecial);
                }else{
                    //新增
                    userGoodsSpecial.setGoodsName(goodsService.selectGoodsById(userGoodsSpecial.getGoodsId()).getGoodName());
                    userGoodsSpecialService.insertUserGoodsSpecial(userGoodsSpecial);
                }
            }else{
                //删除
                List<UserGoodsSpecial> list =userGoodsSpecialService.selectUserGoodsSpecialList(userGoodsSpecial);
                if (!list.isEmpty()){
                    userGoodsSpecialService.deleteUserGoodsSpecialById(list.get(0).getId());
                }else{
                    return R.fail("该商品没设置过特价");
                }

            }
        }
        return R.ok();
    }
}
